/**
 * 本地缓存 用于巡检草稿箱
 * @class onSite
 * @constructor
 */

function onSite(losName){
	this.uid = localStorage.getItem('userid')
	this.losName = losName //缓存字段
}
//读取更新本地任务缓存 callback 回调函数
onSite.prototype.reloadTask = function(callback){
	plus.nativeUI.showWaiting("正在加载..");
	var that = this
	mui.baseApp.httpRequst('/api/v2/sbxj/SyncXsdWindList', 
	{
		userid:this.uid
	},
	function(res){
		var status = res.status,
		data = res.data
		if(data){
			localStorage.setItem(that.losName,JSON.stringify(data))
			localStorage.setItem(that.losName+'user',that.uid)
			if(typeof(callback)=='function'){
				console.log(JSON.stringify(data))
				callback(data)
			}
		}
		plus.nativeUI.closeWaiting()
	},
	function(err){
		
	})
}
//读取缓存任务 callback 回调
onSite.prototype.getTask = function(callback){
	var list = localStorage.getItem(this.losName)
	var userid = localStorage.getItem(this.losName+'user')
	if(list && userid==this.uid){
		list = JSON.parse(list)
	}else{
		this.clearTask()
		this.reloadTask(callback)
	}
	callback(list)
}
//获取单条巡视项
onSite.prototype.getEqu = function(scancode){
	var equ = localStorage.getItem('savexcxj') || '[]'
	equ = JSON.parse(equ)
	var index = equ.findIndex(function(ie){
		return ie.xjxArray.scancode = scancode
	})
	return index>-1 ? equ[index] : undefined
}
//获取巡视项
onSite.prototype.getEquipments = function(xsdwindid){
	var equ = localStorage.getItem('savexcxj')
	var list = []
	if(equ){
		try{
			equ = JSON.parse(equ)
			if(Array.isArray(equ)){
				list = equ.filter(function(item){
					return item.xsdwindid == xsdwindid
				})
			}
		}catch(err){
			console.log('数据获取错误')
		}
		
	}
	return list
}
//清除任务列表
onSite.prototype.clearTask = function(){
	localStorage.removeItem(this.losName)
	//localStorage.removeItem("savexcxj")
}
//删除巡检项
onSite.prototype.delEquipment = function(key){
	var equipment = JSON.parse(localStorage.getItem("savexcxj") || '[]');
	var index = equipment.findIndex(function(item){
		return item.userxjId == key
	})
	if(index>-1){
		equipment.splice(index,1)
		localStorage.setItem("savexcxj",JSON.stringify(equipment))
	}
	return index
	
}
//去掉未存草稿的任务
onSite.prototype.screenTask = function(list){
	var equipment = JSON.parse(localStorage.getItem("savexcxj") || '[]');
	var data = list.filter(function(item){
		return equipment.findIndex(function(ie){
			return item.id == ie.xsdwindid
		}) > -1
	})
	return data
}